# get_facebook_url


INPUT_DIR = "./facebook_payload_with_cookie/laptop/"

module GetFacebookURL
  def self.get_all_files
    Common.get_all_files(INPUT_DIR)
  end

  def self.handle_all_files(all_files)
    facebook_url_mapping = {}

    all_files.each do |file_name|
      # return_array = 
      mapping = handle_each_file file_name

      # facebook_id_array = facebook_id_array + return_array.uniq
      mapping.each do |key, value|
        if facebook_url_mapping.has_key? key
          facebook_url_mapping[key] = facebook_url_mapping[key] + value 
        else
          facebook_url_mapping[key] = value 
        end
      end
    end

    # facebook_id_array.uniq
    facebook_url_mapping
  end

  def self.handle_each_file(file_name)
    puts "now handling : " + file_name
    file = File.open INPUT_DIR + file_name, "r"

    facebook_url_mapping = {}

    facebook_url_mapping[:facebook] = 0
    facebook_url_mapping[:facepile] = 0
    facebook_url_mapping[:like] = 0
    facebook_url_mapping[:likebox] = 0
    facebook_url_mapping[:recommendations] = 0
    facebook_url_mapping[:apps] = 0

    begin
      while line = file.readline
        # fields = line.split("|")
        if line.include? "facebook.com"
          facebook_url_mapping[:facebook] = facebook_url_mapping[:facebook] + 1

          if line.include? "/plugins/facepile.php?"
            facebook_url_mapping[:facepile] = facebook_url_mapping[:facepile] + 1
          end
          if line.include? "/plugins/like.php?"
            facebook_url_mapping[:like] = facebook_url_mapping[:like] + 1
          end
          if line.include? "/plugins/likebox.php?"
            facebook_url_mapping[:likebox] = facebook_url_mapping[:likebox] + 1
          end
          if line.include? "/plugins/recommendations.php?"
            facebook_url_mapping[:recommendations] = facebook_url_mapping[:recommendations] + 1
          end
          if line.include? "apps.facebook.com"
            facebook_url_mapping[:apps] = facebook_url_mapping[:apps] + 1
          end
        end
      end
    rescue Exception => e
      puts e.to_s
    end

    facebook_url_mapping
  end

  def self.run
    puts ">> start to get facebook urls ..."

    begin
      all_files = GetFacebookURL.get_all_files      
      all_mapping = GetFacebookURL.handle_all_files all_files

      output = File.open "facebook_laptop_urls.txt", "w"
      all_mapping.each { |key, value| output.puts "#{key.to_s}:#{value}" }
      output.close

      puts ">> end to get facebook IDs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end
end
